// jqgrid表格配置
// 初始化jqGrid
// 操作按钮
function dispatch() {
    var dispatch = "";
    dispatch = "<a href='javascript:;' class='btn btn-primary btn-sm modify dispatchButton' title='派发'>派发</a>";
    return dispatch;
};

function check() {
    var check = "";
    check = "<a href='#qualitycontrol-assignmentGroup-checkDispatch' class='btn btn-primary btn-sm check' title='查看'>查看</a>";
    return check;
};

function pageInit() {
    var parentHeight, parentWidth;
    parentHeight = $("#dispatch-table").height() - 23,
        parentWidth = $("#dispatch-table").width() + 14;
    //创建jqGrid组件
    jQuery("#dispatchList").jqGrid({
        url: contextPath + "/workorder/queryNotAssignWorkOrderJqg",
        datatype: "json",
        height: parentHeight,
        width: parentWidth,
        colNames: ['工单ID', '工单名称', '项目名称', '设备编号', '设备类型ID', '设备类型', '产品线', '施工单位', '进展', '创建时间', '派发', '操作'],
        colModel: [
            {name: 'workOrderId', index: 'workOrderId', sortable: false, hidden: true},
            {name: 'workOrderName', index: 'workOrderName', sortable: false},
            {name: 'projectName', index: 'projectName', sortable: false},
            {name: 'deviceId', index: 'deviceId', sortable: false},
            {name: 'deviceTypeId', index: 'deviceTypeId', sortable: false, hidden: true},
            {name: 'deviceTypeName', index: 'deviceTypeName', sortable: false},
            {name: 'productLineName', index: 'productLineName', sortable: false},
            {name: 'constructionTeamNames', index: 'constructionTeamNames', sortable: false},
            {name: 'progress', index: 'progress', sortable: false},
            {name: 'createTime', index: 'createTime', sortable: false},
            {name: 'dispatch', index: 'workOrderId', sortable: false, formatter: dispatch},
            {name: 'check', index: 'workOrderId', sortable: false, formatter: check}
        ],
        sortname: 'id',
        sortorder: 'desc',
        viewrecords: true,
        multiselect: true,
        rowNum: 20,
        rowList: [20, 50, 100],
        pager: "#gridpager"
    });
    /*创建jqGrid的操作按钮容器*/
    jQuery("#dispatchList").jqGrid('navGrid', '#gridpager', {edit: false, add: false, del: false, search: false});
};
//函数：获取尺寸
var timer = 0;
$(window).resize(function () {
    clearTimeout(timer);
    timer = setTimeout(function () {
        $("#dispatchList").setGridWidth($('#dispatch-table').width() + 12);
        $("#dispatchList").setGridHeight($('#dispatch-table').height() - 23);
    }, 260);
});
// 侧边栏缩小时，实时的给jqgrid的宽和高赋值
$("#asidebarSwitch").on('click', function () {
    var time = 0;
    var set = setInterval(function () {
        $("#dispatchList").setGridWidth($('#dispatch-table').width() * 1 + 14);
        $("#dispatchList").setGridHeight($('#dispatch-table').height() * 1 - 23);
        time++;
        if (time == 201) {
            clearInterval(set);
        }
    }, time);
});

// 查询区域
// 查询所有产品线
$.ajax({
    url: contextPath + "/ProductLine/queryAllProductLine",
    type: 'GET',
    dataType: "json",
    contentType: "application/json",
    success: function (data) {
        var str = '<option value="" selected>选择产品线</option>';
        for (var i = 0, len = data.length; i < len; i++) {
            str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
        }
        $("#productLineId").html(str);
    },
    error: function (data) {
        toastr.error(data.message, "请求失败");
    }
});
// 查询设备类型
$.ajax({
    url: contextPath + "/DeviceType/queryAllDeviceType",
    type: 'GET',
    dataType: "json",
    contentType: "application/json",
    success: function (data) {
        var str = '<option value="" selected>选择设备类型</option>';
        for (var i = 0, len = data.length; i < len; i++) {
            str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
        }
        $("#deviceTypeId").html(str);
    },
    error: function (data) {
        toastr.error(data.message, "请求失败");
    }
});
// 查询
$('#query').on('click', function () {
    doQuery();
});

function doQuery() {
    var productLineId = $("#productLineId").val() ? Number($('#productLineId').val()) : null;
    var projectName = $("#projectName").val();
    var constructionTeamName = $("#constructionTeamName").val();
    var deviceName = $("#deviceName").val();
    var deviceTypeId = $("#deviceTypeId").val() ? Number($('#deviceTypeId').val()) : null;
    $("#dispatchList").setGridParam({
        url: contextPath + "/workorder/queryNotAssignWorkOrderJqg",
        postData: {
            queryCond: {
                'productLineId': productLineId,
                'projectName': projectName,
                'constructionTeamName': constructionTeamName,
                'deviceName': deviceName,
                'deviceTypeId': deviceTypeId,
                'workOrderState': null
            }
        },
        page: 1
    }).trigger("reloadGrid");
}

// 派发工单
// 单一派发
$("#dispatchList").on('click', '.dispatchButton', function () {
    var parentId = $(this).parent().parent().attr('id');
    var datas = $("#dispatchList").jqGrid('getRowData', parentId);
    var workOrderId = new Array();
    workOrderId.push(datas.workOrderId);
    console.log(workOrderId);
    var workOrderIds = workOrderId.join(",");
    $.ajax({
        url: contextPath + "/workorder/assignWorkOrders",
        type: 'POST',
        data: {'workOrderIds': workOrderIds},
        success: function (data) {
            if (data.code == 1) {
                toastr.success('派发成功!');
            } else {
                toastr.error(data.message, '派发失败');
            }
            doQuery();
        },
        error: function (data) {
            toastr.error(data.message, "派发失败");
        }
    })
});
// 批量派发
$("#dispatchButton").on('click', function () {
    var array = jQuery("#dispatchList").getGridParam('selarrrow');
    if (array.length == 0) {
        toastr.error('请选择需要派发的工单！');
    } else {
        var workOrderIds = new Array();
        $(array).each(function (index, workOrderId) { //遍历每个id 找到每个data 并把属性加到初始化数组里
            var rowData = $("#dispatchList").jqGrid("getRowData", workOrderId);
            workOrderIds.push(rowData.workOrderId);
        });
        var workOrderIds = workOrderIds.join(",");
        $.ajax({
            url: contextPath + "/workorder/assignWorkOrders",
            type: 'POST',
            data: {'workOrderIds': workOrderIds},
            success: function (data) {
                if (data.code == 1) {
                    $("#dispatchList").jqGrid().setGridParam({datatype: 'json', page: 1}).trigger("reloadGrid");
                    toastr.success('派发成功!');
                } else {
                    toastr.error(data.message, '派发失败');
                }
                doQuery();
            },
            error: function (data) {
                toastr.error(data.message, "派发失败");
            }
        })
    }
});

// 查看单个工单
$("#dispatchList").on('click', '.check', function () {
    var id = $(this).parent().parent().attr('id');
    var workOrderData = $("#dispatchList").jqGrid('getRowData', id);
    $.cookie('workOrderId', JSON.stringify(workOrderData.workOrderId));
});
